<?xml version="1.0" encoding="utf-8"?>
<!--
                                                                                     
 h       t     t                ::       /     /                     t             / 
 h       t     t                ::      //    //                     t            // 
 h     ttttt ttttt ppppp sssss         //    //  y   y       sssss ttttt         //  
 hhhh    t     t   p   p s            //    //   y   y       s       t          //   
 h  hh   t     t   ppppp sssss       //    //    yyyyy       sssss   t         //    
 h   h   t     t   p         s  ::   /     /         y  ..       s   t    ..   /     
 h   h   t     t   p     sssss  ::   /     /     yyyyy  ..   sssss   t    ..   /     
                                                                                     
	<https://y.st./>
	Copyright © 2017 Alex Yst <mailto:copyright@y.st>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <https://www.gnu.org./licenses/>.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="https://y.st./en/coursework/CS1103/Unit1.java.xhtml" />
		<title>Unit1.java &lt;https://y.st./en/coursework/CS1103/Unit1.java.xhtml&gt;</title>
		<link rel="icon" type="image/png" href="/link/CC_BY-SA_4.0/y.st./icon.png" />
		<link rel="stylesheet" type="text/css" href="/link/basic.css" />
		<link rel="stylesheet" type="text/css" href="/link/site-specific.css" />
		<script type="text/javascript" src="/script/javascript.js" />
		<meta name="viewport" content="width=device-width" />
	</head>
	<body>
		<nav>
			<p>
				<a href="/en/">Home</a> |
				<a href="/en/a/about.xhtml">About</a> |
				<a href="/en/a/contact.xhtml">Contact</a> |
				<a href="/a/canary.txt">Canary</a> |
				<a href="/en/URI_research/"><abbr title="Uniform Resource Identifier">URI</abbr> research</a> |
				<a href="/en/opinion/">Opinions</a> |
				<a href="/en/coursework/">Coursework</a> |
				<a href="/en/law/">Law</a> |
				<a href="/en/a/links.xhtml">Links</a> |
				<a href="/en/coursework/CS1103/Unit1.java.xhtml.asc">{this page}.asc</a>
			</p>
			<hr/>
			<p>
				<a href="/en/coursework/BUS1101/" title="Principles of Business Management">BUS 1101</a> |
				<span class="hyperlink_unavailable" title="Basic Accounting">BUS 1102</span> |
				<span class="hyperlink_unavailable" title="Principles of Marketing">BUS 2201</span> |
				<span class="hyperlink_unavailable" title="Multinational Management">BUS 2207</span> |
				<span class="hyperlink_unavailable" title="Business and Society">BUS 3306</span> |
				<a href="/en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a> |
				<a href="/en/coursework/CS1102/" title="Programming 1">CS 1102</a> |
				<a href="/en/coursework/CS1103/" title="Programming 2">CS 1103</a> |
				<span class="hyperlink_unavailable" title="Computer Systems">CS 1104</span> |
				<a href="/en/coursework/CS2203/" title="Databases 1">CS 2203</a> |
				<span class="hyperlink_unavailable" title="Communications and Networking">CS 2204</span> |
				<a href="/en/coursework/CS2205/" title="Web Programming 1">CS 2205</a> |
				<a href="/en/coursework/CS2301/" title="Operating Systems 1">CS 2301</a> |
				<span class="hyperlink_unavailable" title="Software Engineering 1">CS 2401</span> |
				<span class="hyperlink_unavailable" title="Data Structures">CS 3303</span> |
				<span class="hyperlink_unavailable" title="Analysis of Algorithms">CS 3304</span> |
				<span class="hyperlink_unavailable" title="Web Programming 2">CS 3305</span> |
				<span class="hyperlink_unavailable" title="Databases 2">CS 3306</span> |
				<span class="hyperlink_unavailable" title="Operating Systems 2">CS 3307</span> |
				<span class="hyperlink_unavailable" title="Information Retrieval">CS 3308</span> |
				<span class="hyperlink_unavailable" title="Comparative Programming Languages">CS 4402</span> |
				<span class="hyperlink_unavailable" title="Software Engineering 2">CS 4403</span> |
				<span class="hyperlink_unavailable" title="Advanced Networking and Data Security">CS 4404</span> |
				<span class="hyperlink_unavailable" title="Mobile Applications">CS 4405</span> |
				<span class="hyperlink_unavailable" title="Computer Graphics">CS 4406</span> |
				<span class="hyperlink_unavailable" title="Data Mining and Machine Learning">CS 4407</span> |
				<span class="hyperlink_unavailable" title="Artificial Intelligence">CS 4408</span> |
				<span class="hyperlink_unavailable" title="English Composition 2">ENGL 1102</span> |
				<span class="hyperlink_unavailable" title="World Literature">ENGL 1405</span> |
				<span class="hyperlink_unavailable" title="Introduction to Environmental Science">ENVS 1301</span> |
				<a href="/en/coursework/HIST1421/" title="Greek and Roman Civilization">HIST 1421</a> |
				<span class="hyperlink_unavailable" title="College Algebra">MATH 1201</span> |
				<span class="hyperlink_unavailable" title="Calculus">MATH 1211</span> |
				<span class="hyperlink_unavailable" title="Introduction to Statistics">MATH 1280</span> |
				<span class="hyperlink_unavailable" title="Discrete Mathematics">MATH 1302</span> |
				<span class="hyperlink_unavailable" title="Introduction to Philosophy">PHIL 1402</span> |
				<a href="/en/coursework/PHIL1404/" title="Ethics and Social Responsibility">PHIL 1404</a> |
				<a href="/en/coursework/POLS1503/" title="Globalization">POLS 1503</a> |
				<span class="hyperlink_unavailable" title="Introduction to Psychology">PSYC 1504</span> |
				<a href="/en/coursework/UNIV1001/" title="Online Education Strategies">UNIV 1001</a>
			</p>
			<hr/>
			<p>
				<a href="/en/coursework/CS1103/Unit1.java.xhtml" title="Unit1.java">Unit 1</a> |
				<a href="/en/coursework/CS1103/SimpleRandomSentences.java.xhtml" title="SimpleRandomSentences.java">Unit 2</a> |
				<a href="/en/coursework/CS1103/Tape.java.xhtml" title="Tape.java">Unit 3</a> |
				<a href="/en/coursework/CS1103/Debugging.xhtml" title="Debugging">Unit 4</a> |
				<a href="/en/coursework/CS1103/Unit5.java.xhtml" title="Unit5.java">Unit 5</a> |
				<a href="/en/coursework/CS1103/DrawTextPanel.java.xhtml" title="DrawTextPanel.java">Unit 6</a> |
				<a href="/en/coursework/CS1103/Unit7.java.xhtml" title="Unit7.java">Unit 7</a>
			</p>
			<hr/>
		</nav>
		<header>
			<h1>Unit1.java</h1>
			<p>Written in <span title="Programming 2">CS 1103</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-06-21</p>
		</header>
<h2>Source code:</h2>
<blockquote>
<pre><code>/* 
 * Copyright (C) 2017 Alex Yst
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
 */
/* 
 * Output of this program:
 * Seconds needed for insertionSort() to sort 1000 elements: 0.011
 * Seconds needed for selectionSort() to sort 1000 elements: 0.008
 * Seconds needed for java.util.Arrays.sort() to sort 1000 elements: 0.001
 * Seconds needed for insertionSort() to sort 10000 elements: 0.097
 * Seconds needed for selectionSort() to sort 10000 elements: 0.183
 * Seconds needed for java.util.Arrays.sort() to sort 10000 elements: 0.001
 * Seconds needed for insertionSort() to sort 100000 elements: 2.766
 * Seconds needed for selectionSort() to sort 100000 elements: 10.424
 * Seconds needed for java.util.Arrays.sort() to sort 100000 elements: 0.004
 */
package st.y.CS1103.Unit1;

/**
 * Alex Yst&apos;s CS 1103 (Programming 2) Unit 1 submission
 * 
 * This class compares three sorting algorithms; two defined in user space and
 * one built into the language.
 * 
 * @author Alex Yst
 */
public class Unit1 {
/**
 * This is the list of array lengths we&apos;ll test with.
 */
	final static int[] ARRAY_LENGTHS = {
		1000,
		10000,
		100000,
	};

/**
 * Main method
 * 
 * Sorting algorithms are compared here and their completion times are output.
 * 
 * @param args The command line arguments are ignored.
 */
	public static void main(String[] args) {
// We need to test this with three different lengths. Why not use a loop?
		for(int length : ARRAY_LENGTHS) {
			int[] insertionArray = new int[length];
			int[] selectionArray = new int[length];
			int[] regularArray = new int[length];
			for(int i=0;i&lt;length;i++) {
				int integer = (int)(Integer.MAX_VALUE * Math.random());
// These arrays should be identical to prevent any strangeness that could make
// one sorting algorithm falsely appear faster than it is.
				insertionArray[i] = integer;
				selectionArray[i] = integer;
				regularArray[i] = integer;
			}
			long insertionStartTime = System.currentTimeMillis();
			insertionSort(insertionArray);
			long insertionRunTime = System.currentTimeMillis() - insertionStartTime;
			System.out.print(&quot;Seconds needed for insertionSort() to sort &quot;);
			System.out.print(length);
			System.out.print(&quot; elements: &quot;);
			System.out.println(insertionRunTime/1000.0);
			long selectionStartTime = System.currentTimeMillis();
			selectionSort(selectionArray);
			long selectionRunTime = System.currentTimeMillis() - selectionStartTime;
			System.out.print(&quot;Seconds needed for selectionSort() to sort &quot;);
			System.out.print(length);
			System.out.print(&quot; elements: &quot;);
			System.out.println(selectionRunTime/1000.0);
			long regularStartTime = System.currentTimeMillis();
			java.util.Arrays.sort(insertionArray);
			long regularRunTime = System.currentTimeMillis() - regularStartTime;
			System.out.print(&quot;Seconds needed for java.util.Arrays.sort() to sort &quot;);
			System.out.print(length);
			System.out.print(&quot; elements: &quot;);
			System.out.println(regularRunTime/1000.0);
		}
	}

/**
 * Insertion Sort
 * 
 * This method uses an insertion sort to sort elements in an array of integers.
 * No value is returned and the original array is modified.
 * 
 * This method was copied from
 * &lt;http://math.hws.edu./eck/cs124/javanotes5/c7/s4.html&gt;. THis method, along
 * with the rest of the book that contains it, was released under the CC BY-SA
 * 2.5 license. According to the Creative Commons compatibility list, CC BY-SA
 * 2.5 works may be upgraded to be covered by version 4.0 of the same license,
 * and works under CC BY-SA are compatible with the GNU GPLv3. It is therefore
 * my understanding that I may incorporate this method into this GPLv3 class.
 * If you feel this is incorrect, please let me know and I&apos;ll do my best to
 * remedy the situation. For more information, the Creative Commons
 * compatibility list is available here:
 * &lt;https://creativecommons.org/share-your-work/licensing-considerations/compatible-licenses/&gt;.
 * 
 * @author David J. Eck
 * @param A The array to be sorted
 */
	public static void insertionSort(int[] A) {
// Sort the array A into increasing order.

		int itemsSorted; // Number of items that have been sorted so far.

		for (itemsSorted = 1; itemsSorted &lt; A.length; itemsSorted++) {
// Assume that items A[0], A[1], ... A[itemsSorted-1] 
// have already been sorted.  Insert A[itemsSorted]
// into the sorted part of the list.

			int temp = A[itemsSorted];  // The item to be inserted.
			int loc = itemsSorted - 1;  // Start at end of list.

			while (loc &gt;= 0 &amp;&amp; A[loc] &gt; temp) {
				A[loc + 1] = A[loc]; // Bump item from A[loc] up to loc+1.
				loc = loc - 1;       // Go on to next location.
			}

			A[loc + 1] = temp; // Put temp in last vacated space.
		}
	}

/**
 * Selection Sort
 * 
 * This method uses a selection sort to sort elements in an array of
 * integers. No value is returned and the original array is modified.
 * 
 * This method was copied from
 * &lt;http://math.hws.edu./eck/cs124/javanotes5/c7/s4.html&gt;. THis method, along
 * with the rest of the book that contains it, was released under the CC BY-SA
 * 2.5 license. According to the Creative Commons compatibility list, CC BY-SA
 * 2.5 works may be upgraded to be covered by version 4.0 of the same license,
 * and works under CC BY-SA are compatible with the GNU GPLv3. It is therefore
 * my understanding that I may incorporate this method into this GPLv3 class.
 * If you feel this is incorrect, please let me know and I&apos;ll do my best to
 * remedy the situation. For more information, the Creative Commons
 * compatibility list is available here:
 * &lt;https://creativecommons.org/share-your-work/licensing-considerations/compatible-licenses/&gt;.
 * 
 * @author David J. Eck
 * @param A The array to be sorted
 */
	public static void selectionSort(int[] A) {
// Sort A into increasing order, using selection sort

		for (int lastPlace = A.length-1; lastPlace &gt; 0; lastPlace--) {
// Find the largest item among A[0], A[1], ...,
// A[lastPlace], and move it into position lastPlace 
// by swapping it with the number that is currently 
// in position lastPlace.

			int maxLoc = 0; // Location of largest item seen so far.

			for (int j = 1; j &lt;= lastPlace; j++) {
				if (A[j] &gt; A[maxLoc]) {
// Since A[j] is bigger than the maximum we&apos;ve seen
// so far, j is the new location of the maximum value
// we&apos;ve seen so far.
					maxLoc = j;
				}
			}

			int temp = A[maxLoc]; // Swap largest item with A[lastPlace].
			A[maxLoc] = A[lastPlace];
			A[lastPlace] = temp;

		} // end of for loop

	}
}</code></pre>
</blockquote>
		<hr/>
		<p>
			Copyright © 2017 Alex Yst;
			You may modify and/or redistribute this document under the terms of the <a rel="license" href="/license/gpl-3.0-standalone.xhtml"><abbr title="GNU&apos;s Not Unix">GNU</abbr> <abbr title="General Public License version Three or later">GPLv3+</abbr></a>.
			If for some reason you would prefer to modify and/or distribute this document under other free copyleft terms, please ask me via email.
			My address is in the source comments near the top of this document.
			This license also applies to embedded content such as images.
			For more information on that, see <a href="/en/a/licensing.xhtml">licensing</a>.
		</p>
		<p>
			<abbr title="World Wide Web Consortium">W3C</abbr> standards are important.
			This document conforms to the <a href="https://validator.w3.org./nu/?doc=https%3A%2F%2Fy.st.%2Fen%2Fcoursework%2FCS1103%2FUnit1.java.xhtml"><abbr title="Extensible Hypertext Markup Language">XHTML</abbr> 5.1</a> specification and uses style sheets that conform to the <a href="http://jigsaw.w3.org./css-validator/validator?uri=https%3A%2F%2Fy.st.%2Fen%2Fcoursework%2FCS1103%2FUnit1.java.xhtml"><abbr title="Cascading Style Sheets">CSS</abbr>3</a> specification.
		</p>
	</body>
</html>

